
package com.jiu.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import com.jiu.core.FishContext;

/**
 * 主菜单窗口
 */

public class MainFrame extends BaseFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 815705495599703503L;
	private JTextPane textPane;
	private JPanel stackPanel ;
	private FishContext context;
	private boolean isRoll = true;
	private JCheckBox stateBox;
	private JTextPane sendText;

	public MainFrame(FishContext context) {
		this.context = context;
		init();
	}
	
	// 初始化窗口
	private void init() {
		this.setSize(640, 520);
		this.setLocationRelativeTo(null);
		this.setContentPane(createContentPane());
		this.setJMenuBar(new MyMenuBar(this,context));
	}

	// 创建主pane
	private JPanel createContentPane() {
		JPanel panel = new JPanel(new BorderLayout());
		// 创建一张图片 图表
		panel.add(createMenuPane(), BorderLayout.CENTER);
		return panel;
	}
	
	// 创建菜单项的panel
	private JPanel createMenuPane() {
		JPanel panel = new JPanel(new BorderLayout());
		// 将来这个label 是会变的！！！！！！！！！！！！！！！！！！！！！！
		panel.add(createStackPanel(), BorderLayout.NORTH);
		panel.add(createTextpanel(), BorderLayout.CENTER);
		panel.add(createSendPanel(), BorderLayout.SOUTH);
		return panel;
	}
	private Component createStackPanel() {
		stackPanel = new JPanel(new GridLayout(1, 5, 20, 20));
		JLabel label1 = new JLabel("钓鱼命令");
		JLabel label2 = new JLabel("售鱼命令");
		JLabel label3 = new JLabel("新命令");
		JLabel label4 = new JLabel("其他命令");
		JLabel label5 = new JLabel("控制命令");
		stackPanel.setBackground(new Color(255,255,200));
		stackPanel.add(label1);
		stackPanel.add(label2);
		stackPanel.add(label3);
		stackPanel.add(label4);
		stackPanel.add(label5);
		return stackPanel;
	}

	private Component createTextpanel() {
		JPanel panel = new JPanel(new BorderLayout());
		textPane = new TextPaneMenu(context);
		JScrollPane scrollPane = new JScrollPane(textPane);
		panel.add(scrollPane, BorderLayout.CENTER);		
		return panel;
	}
	
	
	public void updateStackMessage(Map<Integer, String> sendMap) {
		for(int i=1;i<=5;i++){
			String text = sendMap.get(i)==null?"(空)":sendMap.get(i);
			((JLabel)stackPanel.getComponent(i-1)).setText(text);
		}
	}

	private SimpleAttributeSet as = new SimpleAttributeSet();
	public void updateDisplayMessage(String msg, Color color) {
		StyleConstants.setForeground(as, color);
		StyleConstants.setFontSize(as, 12);
		Document doc = textPane.getDocument();
		try {
			doc.insertString(doc.getLength(), msg + "\n", as);
			if (doc.getLength() > 20000)
				doc.remove(0, 2000);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		if (isRoll)
			textPane.setCaretPosition(doc.getLength());
	}
	public void updateDisplayMessage(List<Map<String,Object>> list) {
		Document doc = textPane.getDocument();
		try {
			for(Map<String,Object> map:list){
				if(map.get("color")!=null)
					StyleConstants.setForeground(as, (Color) map.get("color"));
				if(map.get("size")!=null)
					StyleConstants.setFontSize(as, (Integer) map.get("size"));
				if(map.get("bgColor")!=null)
					StyleConstants.setBackground(as, (Color) map.get("bgColor"));
				doc.insertString(doc.getLength(), map.get("msg").toString(), as);			
			}
			doc.insertString(doc.getLength(),"\n",as);
			if (doc.getLength() > 50000)
				doc.remove(0, 5000);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		if (isRoll)
			textPane.setCaretPosition(doc.getLength());
	}
	/**
	 * 发消息
	 */
	private Component createSendPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		sendText = new JTextPane();
		JScrollPane scrollPane = new JScrollPane(sendText);
		sendText.setBackground(new Color(200,255,255));
		JButton sendButton = null;
		try {
			sendButton = new JButton(
					new ImageIcon(ImageIO.read(getClass().getResourceAsStream("image/send.png"))));
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		sendButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendText();
			}

		});
		sendText.addKeyListener(new KeyListener() {
			public void keyTyped(KeyEvent e) {			
			}
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					sendText();
					sendText.setCaretPosition(sendText.getText().length());
				}	
			}
			public void keyPressed(KeyEvent e) {								
			}
		});
		panel.add(createCheckBox(),BorderLayout.WEST);
		panel.add(scrollPane, BorderLayout.CENTER);
		panel.add(sendButton, BorderLayout.EAST);
		return panel;
	}
	private void sendText() {
		String text = sendText.getText();
		text = text.length()>10?text.substring(0,10)+"..":text;
		context.getSendMap().put(5, sendText.getText());
		updateStackMessage(context.getSendMap());
		sendText.setText("#D ");
	}
	
	


	private Component createCheckBox() {
		JPanel panel = new JPanel(new BorderLayout());
		final JCheckBox roll = new JCheckBox("滚屏");
		stateBox = new JCheckBox("状态");
		roll.setSelected(true);
		stateBox.setSelected(false);
		roll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (roll.isSelected())
					isRoll = true;
				else
					isRoll = false;
			}
		});
		stateBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (stateBox.isSelected())
					context.updateStatus(false);
				else
					context.updateStatus(true);
			}
		});
		panel.add(stateBox,BorderLayout.NORTH);
		panel.add(roll,BorderLayout.SOUTH);
		return panel;
	}

	public JTextPane getTextPane() {
		return textPane;
	}

	public void updateStateBox(boolean state) {
		stateBox.setSelected(!state);
	}

	public JTextPane getSendText() {
		return sendText;
	}

	@Override
	protected void exit(JFrame frame) {		
		int value = JOptionPane.showConfirmDialog(frame, "您确认要退出吗？");
		if (value == JOptionPane.YES_OPTION) {
			if(context.getAccount().getPassword()!=null&&!context.getAccount().isStop()){
				this.setTitle("正在退出请稍候...");			
				context.getFishHttpService().LogOut();
				try {Thread.sleep(1000);} catch (InterruptedException e) {}									
			}			
			System.exit(0);
		}
	}
	
}
